package com.culture.bootdemo.model.req;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: wen
 * @date 2020/10/9
 */

public class PhoneValidator implements ConstraintValidator<PhoneValidatorAnnotation, String> {

    private boolean required = false;

    public static final Pattern PHONE_REGEXP = Pattern.compile("^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}$");

    @Override
    public void initialize(PhoneValidatorAnnotation phoneValidatorAnnotation) {
        required = phoneValidatorAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return false;
        }
        Matcher matcher = PHONE_REGEXP.matcher(value);
        return matcher.matches();

    }

    public static void main(String[] args) {
        String str="3434.1";
        Pattern pattern = Pattern.compile("[+]?[0-9]+(\\.[0-9]+)?");
        boolean matches = pattern.matcher(str).matches();
        System.out.println(matches);
    }
}

